package com.ww.web;

import com.alibaba.fastjson.JSON;
import com.ww.dao.GoodsDao;
import com.ww.dao.TrolleyDao;
import com.ww.dao.UserDao;
import com.ww.dao.impl.GoodsDaoImpl;
import com.ww.dao.impl.TrolleyDaoImpl;
import com.ww.dao.impl.UserDaoImpl;
import com.ww.entity.Goods;
import com.ww.entity.Trolley;
import com.ww.util.UUIDUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

@WebServlet("/goods/*")
public class GoodsServlet extends HttpServlet {
    private GoodsDao goodsDao=new GoodsDaoImpl();
    private TrolleyDao trolleyDao=new TrolleyDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String baseURL = req.getContextPath();//获取项目的基本路径
        String uri = req.getRequestURI();
        String path = uri.substring(uri.lastIndexOf("/") + 1);
        if ("type.do".equals(path)){
            List<Goods> typelist = goodsDao.findType();
            req.setAttribute("typelist",typelist);
            req.getRequestDispatcher(baseURL+"/category.jsp").forward(req,resp);
            return;
        }else if ("list.do".equals(path)){
            list(req, resp);
            return;
        }else if ("detail.do".equals(path)) {
            String name = req.getParameter("name");
            Goods goods = goodsDao.findByName(name);
            req.setAttribute("goods",goods);
            req.getRequestDispatcher(baseURL+"/detail.jsp").forward(req,resp);
            return;
        }else if ("shopadd.do".equals(path)) {
            shopadd(req, resp, baseURL);
            return;
        }else if ("newproduct.do".equals(path)) {
            List<Goods> goodsList = goodsDao.findAll();
            req.setAttribute("goodsList",goodsList);
            req.getRequestDispatcher(baseURL+"/newproduct.jsp").forward(req,resp);
            return;
        }else if ("specialprice.do".equals(path)) {
            List<Goods> goodsList = goodsDao.findAll();
            req.setAttribute("goodsList",goodsList);
            req.getRequestDispatcher(baseURL+"/specialprice.jsp").forward(req,resp);
            return;
        }else if ("goodsindex.do".equals(path)) {
            List<Goods> goodsList = goodsDao.findPage(1,3);
            resp.setContentType("application/json;charset=utf-8");
            String json = JSON.toJSONString(goodsList);
            PrintWriter out = resp.getWriter();
            out.write(json);
            out.close();
            return;
        }else if ("goods_reserve.do".equals(path)) {
            List<Goods> goodsList = goodsDao.findAll();
            req.setAttribute("goodsList",goodsList);
            req.getRequestDispatcher(baseURL+"/reserve.jsp").forward(req,resp);
            return;
        }else {
            throw new RuntimeException("请求错误");
        }
    }

    private void shopadd(HttpServletRequest req, HttpServletResponse resp, String baseURL) throws ServletException, IOException {
        String name = req.getParameter("name");
        Goods goods = goodsDao.findByName(name);
        int num = Integer.parseInt(req.getParameter("num"));
        HttpSession session = req.getSession();
        String user_id = null;
        if (session.getAttribute("user_id")!=null) {
            user_id = session.getAttribute("user_id").toString();
        }

        Trolley trolley=new Trolley();
        trolley.setUser_id(user_id);
        trolley.setTrolley_id(UUIDUtils.getUUID());
        trolley.setGoods_id(goods.getGoods_id());
        trolley.setTrolley_num(num);
        trolley.setTrolley_status("加入购物车");
        List<Trolley> trolleyList = trolleyDao.findAll();
        boolean a=true;
        for (Trolley trolley1 : trolleyList) {
            if (trolley1.getGoods_id().equals(goods.getGoods_id())){
                Trolley trolley2 = trolleyDao.isRepeat(trolley.getUser_id(), trolley.getGoods_id());
                num=trolley2.getTrolley_num()+num;
                trolleyDao.updateTrolley(trolley2.getTrolley_id(),num);
                a=false;
            }
        }
        if (a){
            trolleyDao.save(trolley);
        }

        req.getRequestDispatcher(baseURL+"/trolley/list.do").forward(req,resp);
        return;
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String type = req.getParameter("type");
        List<Goods> list = goodsDao.findList(type);
        resp.setContentType("application/json;charset=utf-8");
        String json = JSON.toJSONString(list);
        PrintWriter out = resp.getWriter();
        out.write(json);
        out.close();
        return;
    }
}
